/*
 * EmployeeAvailability.java
 * 
 * Copyright (c) 2008, Marshall Scorcio, Ralph Holzmann, 
 *                     Stewart Sweet, Tyler Hoersch, Jeffery Van Handel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the University of Wisconsin Oshkosh nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

import java.util.Vector;

public class EmployeeAvailability implements java.io.Serializable
{
    private Vector<RequestedDayOff> requestedDaysOff = new Vector<RequestedDayOff>();
    private Vector<Day> weeklyAvailability = new Vector<Day>(7);
    
    public void EmployeeAvailability(Vector<Day> daysAvailable) 
    {
        weeklyAvailability = daysAvailable;
    }
    
    public void addRequestedDayOff(RequestedDayOff day)
    {        
        requestedDaysOff.add(day);
    }
    
    public void changeDayRequestedOff(RequestedDayOff oldDay, RequestedDayOff newDay)
    {
        if (requestedDaysOff.contains(oldDay))
        {
            cancelRequestedDayOff(oldDay);
            addRequestedDayOff(newDay);
        }
    }
    
    public RequestedDayOff findDayByDate(int dayOfWeek, int dayOfMonth, int month, int year)
    {        
        RequestedDayOff day = null;
        
        RequestedDayOff date = new RequestedDayOff(dayOfWeek, dayOfMonth, month, year);
        
        for (int i = 0; i < requestedDaysOff.size(); i++)
        {
            if (requestedDaysOff.elementAt(i).equals(date))
            {
               day = requestedDaysOff.elementAt(i);
            }
        }
        
        return day;
    }
    
    public void cancelRequestedDayOff(RequestedDayOff day)
    {
        if (requestedDaysOff.contains(day))
        {
            requestedDaysOff.remove(day);
        }
    }
    
    public void clearRequestedDaysOff()
    {
        requestedDaysOff.clear();
    }
    
    public boolean hasRequestedDays()
    {
        return (requestedDaysOff.size() != 0);
    }
    
    public int getNumberOfRequestedDaysOff()
    {
        return requestedDaysOff.size();
    }

    public Vector<RequestedDayOff> getRequestedDaysOff()
    {
        return requestedDaysOff;
    }

    public void addDailyAvailability(Day day)
    {
        weeklyAvailability.add(day);
    }
    
    public void removeDailyAvailability(int day) 
    {
        weeklyAvailability.remove(day);
    }
    
    public void clearWeeklyAvailability()
    {
        weeklyAvailability.clear();
    }
    
    public void replaceDailyAvailability(int day, Day newDay)
    {
        weeklyAvailability.setElementAt(newDay, day);
    }
    
    public Vector<Day> getWeeklyAvailability()
    {
        return weeklyAvailability;
    }
    
    public Day getDailyAvailabilityFor(int day)
    {        
        return weeklyAvailability.elementAt(day);
    }
    
    public boolean isAvailableForShift(Day day, Shift shift)
    {
        boolean available = false;

        Vector<TimeBlock> timesAvailable = getDailyAvailabilityFor(day.getDayOfWeek()).getTimeBlocks();

        int i = 0;
        while (i < timesAvailable.size() && available == false)
        {
            TimeBlock timeAvailable = timesAvailable.elementAt(i);
            
            if (timeAvailable.contains(shift))
            {
                available = true;
            }
            i++;
        }
        
        i = 0;
        while (i < requestedDaysOff.size() && available == true)
        {
            if (requestedDaysOff.elementAt(i).equals(day))
            {
                Vector<TimeBlock> timesRequestedOff = requestedDaysOff.elementAt(i).getTimeBlocks();
                
                int j = 0;
                while (j < timesRequestedOff.size() && available == true)
                {
                    TimeBlock timeRequestedOff = timesRequestedOff.elementAt(j);
                    
                    if (timeRequestedOff.overlaps(shift))
                    {
                        available = false;
                    }
                    j++;
                }
            }
            i++;
        }

        return available;
    }
}
